package com.atugui.test;

import com.atguigu.bean.Person;
import com.atguigu.config.MainConfigOfPropertyValues;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;


public class IOCTest_PropertyValue {
    AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfPropertyValues.class);

    @Test
    public void test01() {
        //1.创建IOC容器
        System.out.println("容器创建完成......");
        printBeans(applicationContext);
        System.out.println("============");
        Person person = (Person)applicationContext.getBean("person");
        System.out.println(person);

        //获取环境变量的配置
        ConfigurableEnvironment environment = applicationContext.getEnvironment();
        //配置文件中的值,默认都加载到了环境变量里
        String property = environment.getProperty("person.nickName");
        System.out.println(property);


        //关闭容器
        applicationContext.close();

    }

    private void printBeans(AnnotationConfigApplicationContext annotationConfigApplicationContext) {
        String[] beanDefinitionNames = annotationConfigApplicationContext.getBeanDefinitionNames();
        for (String beanDefinitionName : beanDefinitionNames) {
            System.out.println(beanDefinitionName);
        }
    }

}
